package com.hoyue.servlet;

import com.hoyue.bean.MessageBoard;
import com.hoyue.dao.impl.MessageBoardDaoImpl;
import com.hoyue.service.MessageBoardService;
import com.hoyue.utils.C3P0Utils;
import com.hoyue.utils.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/MessageBoardServlet")
public class MessageBoardServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("method");//获取方法名
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("没有传递method参数,请给出你想调用的方法");
        }
        Class c = this.getClass();//获得当前类的Class对象
        Method method = null;
        try {
            //获得Method对象
            method = getClass().getDeclaredMethod(name, HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
            throw new RuntimeException("没有找到" + name + "方法，请检查该方法是否存在");
        }

        try {
            method.invoke(this, request, response);//反射调用方法
        } catch (Exception e) {
            System.out.println("你调用的方法" + name + ",内部发生了异常");
            throw new RuntimeException(e);
        }
    }

    MessageBoardService messageBoardService = new MessageBoardService();

    protected void adminGetMessageBoard(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page1 = request.getParameter("page");

        if(page1 == null || "".equals(page1)) {
            page1 = "1";
        }
        int page = Integer.parseInt(page1);

        Page<MessageBoard> message = messageBoardService.getPage(page);

        request.setAttribute("message",message);

        request.getRequestDispatcher("/WEB-INF/admin/join.jsp").forward(request,response);
    }

    protected void addMessageBoard(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        MessageBoard messageBoard = new MessageBoard();

        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String message = request.getParameter("message");
        messageBoard.setName(name);
        messageBoard.setEmail(email);
        messageBoard.setPhone(phone);
        messageBoard.setMessage(message);
        messageBoardService.addMessageBoard(messageBoard);
        request.getRequestDispatcher("index.jsp").forward(request,response);
        //request.getRequestDispatcher(request.getContextPath() + "/MessageBoardServlet?method=adminGetMessageBoard").forward(request,response);
    }

    /*单条数据删除*/
    protected void deleteMessageBoard(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {

        int id = Integer.parseInt(request.getParameter("id"));

        messageBoardService.deleteMessageBoard(id);

        request.getRequestDispatcher(request.getContextPath() + "/MessageBoardServlet?method=adminGetMessageBoard").forward(request,response);
    }

    /*批量数据删除*/
    protected void deleteMessageBoards(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //1.获取请求参数
        String ids = request.getParameter("ids");

        //2.调用service
        MessageBoardDaoImpl messageBoardService = new MessageBoardDaoImpl();
        try {
            messageBoardService.deleteMessageBoards(ids);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(request.getContextPath() + "/MessageBoardServlet?method=adminGetMessageBoard").forward(request, response);
    }
}
